package com.xiaoyu.client.ui.fragment.help;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.seek.SeekBean;
import com.xiaoyu.client.model.seek.SeekParam;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.activity.help.HelpDetailActivity;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.adapter.AllPowerfulAdapter;
import com.xiaoyu.commonlib.utils.ToastUtil;

import java.util.List;

//MVP 模式
public class SeekModel {
    public static final String SEEK_ID = "my_seek_seek_id";
    private AllPowerfulAdapter<SeekParam> mSeekAdapter;
    private int state;
    private int solveState;
    private int page = 1;//页数
    private int count = 0;//数据总条数
    private SmartRefreshLayout mRefreshLayout;
    private ImageView mEmptyImg;
    private Context mContext;

    public SeekModel(int state, int solveState, SmartRefreshLayout mRefreshLayout, Context mContext, ImageView mEmptyImg) {
        this.state = state;
        this.solveState = solveState;
        this.mRefreshLayout = mRefreshLayout;
        this.mContext = mContext;
        this.mEmptyImg = mEmptyImg;
        getMyInitiate();
        setObligationAdapter(null);
        mRefreshLayout.setOnRefreshListener(onRefreshListener);
        mRefreshLayout.setOnLoadMoreListener(onLoadMoreListener);
        mRefreshLayout.setNoMoreData(false);
    }

    public ISeekListListener listListener;

    public interface ISeekListListener {
        void seekList(AllPowerfulAdapter mAdapter);
    }

    public void setListListener(ISeekListListener listListener) {
        this.listListener = listListener;
    }

    private void getMyInitiate() {
        NetworkManager.myInitiate(state, solveState, page, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                succeedMyInitiate(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    private void succeedMyInitiate(String result) {
        Log.i("jx", "succeedMyInitiate: 我的求助====" + result);
        SeekBean seekBean = new Gson().fromJson(result, SeekBean.class);
        if (seekBean.getSeekData().getList().size() == 0) {
            mEmptyImg.setVisibility(View.VISIBLE);
            mRefreshLayout.setVisibility(View.GONE);
        }

        if (listListener != null) {
            listListener.seekList(mSeekAdapter);
        }

        mSeekAdapter.setNewData(seekBean.getSeekData().getList());
        count = Integer.parseInt(seekBean.getSeekData().getCount());

    }

    //设置适配器
    private void setObligationAdapter(List<SeekParam> mOrderList) {
        mSeekAdapter = new AllPowerfulAdapter<SeekParam>(R.layout.my_seek_item, mOrderList, new AllPowerfulAdapter.OnClickListener<SeekParam>() {
            @Override
            public void setOnClick(BaseViewHolder baseViewHolder, SeekParam seekParam, int position) {
                Intent intent = new Intent(mContext, HelpDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt(ClientConstants.FROM, ClientConstants.MY_SEEK_FRAGMENT);
                bundle.putString(SEEK_ID, seekParam.getSeekid());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        }) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, SeekParam seekParam) {
                super.convert(baseViewHolder, seekParam);
                baseViewHolder.setText(R.id.my_seek_item_title, seekParam.getSeektitle());
                baseViewHolder.setText(R.id.my_seek_item_time, seekParam.getSeektime().substring(5, 10) + " 发布      回答 " + seekParam.getSeeksum());

                TextView mState = baseViewHolder.getView(R.id.my_seek_item_state);
                if (state != 0) {
                    mState.setVisibility(View.GONE);
                } else {
                    if (seekParam.getSeekstate().equals("0")) {
                        mState.setText("未解决");
                    } else {
                        mState.setText("已解决");
                    }
                }
            }
        };
    }


    //刷新监听
    private OnRefreshListener onRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            page = 1;
            mSeekAdapter.notifyDataSetChanged();
            getMyInitiate();
            mRefreshLayout.finishRefresh();
        }
    };

    //加载更多
    private OnLoadMoreListener onLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
            if (page < count) {
                page = page + 1;
                getMyInitiate();
                mRefreshLayout.finishLoadMore();
                mRefreshLayout.setNoMoreData(false);
            } else {
                mRefreshLayout.finishLoadMore();
                mRefreshLayout.setNoMoreData(true);
                mRefreshLayout.finishLoadMoreWithNoMoreData();
                ToastUtil.showBelowToast(mContext, "已经没有更多数据！！！");
            }
        }
    };
}
